﻿using System;
using System.Collections.Generic;
using System.Threading;
using PPI.Client.Logic;
using PPI.Client.Logic.Inspection;
using PPI.Client.Logic.Polygon;
using PPI.Entities;
using PPI.Entities.Inspections;
using PPI.Entities.Lookup;
using PPI.Entities.Polygon;
using PPI.Entities.Settings;

namespace PPI.Client
{
    public sealed class LCacheDataSingleton
    {
        private static LCacheDataSingleton m_instance;
        static readonly object padlock = new object();

        private LCacheDataSingleton()
        {
            LoadData();
        }

        private void LoadData()
        {
            /*
            m_Class = LXMissionStructures_PoleClass.SelectAllIdName();
            m_Specie = LXMissionStructures_PoleSpecies.SelectAllIdName();
            m_Treatment = LXMissionStructures_PoleTreatments.SelectAllIdName();
            m_Vendor = LVendor.SelectAllIdName();
            m_Maps = LInspectionMap.SelectAll();
            m_Companies = LCompany.SelectAll();
            m_Notes = LNote.SelectAllIdName();
            m_InspectionTypes = LInspectionType.SelectAllIdName();
            m_ResultTypes = LInspectionResultType.SelectAllIdName();
            m_Handhelds = LHandheld.SelectAllIdName();
            m_VisitTypes = LVendor_Visit_Type.SelectAllIdName();
            m_TopTypes = LXMissionStructures_TopType.SelectAllIdName();
            m_Contacts = LVendor_Contacts.SelectAllIdName();
            m_Projects = LVendor_Project.SelectAll();
            m_Polygons = LPolygon.GetAll();
            m_Utilities = LUtility.SelectAllIdName();
            m_customKml = LCustomKml.SelectInspectionKmlSetting();
            m_CentralOffices = LCentralOffice.GetAll();
            */


            thread = new Thread(Load);
            thread.Start();

            thread1 = new Thread(Load1);
            thread1.Start();

            
            thread2 = new Thread(Load2);
            thread2.Start();
            
            /*
            thread3 = new Thread(Load3);
            thread3.Start();
             */
 
            while (thread.IsAlive){}
            while (thread1.IsAlive) { }
            while (thread2.IsAlive) { }
            
            /*
            while (thread3.IsAlive) { }
            */
        }

        private static void Load()
        {
            m_Class = LXMissionStructures_PoleClass.SelectAllIdName();
            m_Specie = LXMissionStructures_PoleSpecies.SelectAllIdName();
            m_Treatment = LXMissionStructures_PoleTreatments.SelectAllIdName();
            m_Vendor = LVendor.SelectAllIdName();
            m_Maps = LInspectionMap.SelectAll();
            m_Companies = LCompany.SelectAll();
            m_Notes = LNote.SelectAllIdName();
        }

        private static void Load1()
        {
            m_ResultTypes = LInspectionResultType.SelectAllIdName();
            m_Handhelds = LHandheld.SelectAllIdName();
            m_VisitTypes = LVendor_Visit_Type.SelectAllIdName();
            m_TopTypes = LXMissionStructures_TopType.SelectAllIdName();
            m_Contacts = LVendor_Contacts.SelectAllIdName();
            m_Projects = LVendor_Project.SelectAll();
        }

        private static void Load2()
        {
            m_InspectionTypes = LInspectionType.SelectAllIdName();
            m_Polygons = LPolygon.GetAll();
            m_Utilities = LUtility.SelectAllIdName();
            m_customKml = LCustomKml.SelectInspectionKmlSetting();
            m_CentralOffices = LCentralOffice.GetAll();            
        }
        
        //private static void Load()
        //{
        //    m_Class = LXMissionStructures_PoleClass.SelectAllIdName();
        //    m_Specie = LXMissionStructures_PoleSpecies.SelectAllIdName();
        //    m_Treatment = LXMissionStructures_PoleTreatments.SelectAllIdName();
        //    m_Vendor = LVendor.SelectAllIdName();
        //    m_Maps = LInspectionMap.SelectAll();
        //}

        //private static void Load1()
        //{
        //    m_Companies = LCompany.SelectAll();
        //    m_Notes = LNote.SelectAllIdName();
        //    m_InspectionTypes = LInspectionType.SelectAllIdName();
        //    m_ResultTypes = LInspectionResultType.SelectAllIdName();
        //    m_Handhelds = LHandheld.SelectAllIdName();
        //}

        //private static void Load2()
        //{
        //    m_VisitTypes = LVendor_Visit_Type.SelectAllIdName();
        //    m_TopTypes = LXMissionStructures_TopType.SelectAllIdName();
        //    m_Contacts = LVendor_Contacts.SelectAllIdName();
        //    m_Projects = LVendor_Project.SelectAll();
        //}

        //private static void Load3()
        //{
        //    m_Polygons = LPolygon.GetAll();
        //    m_Utilities = LUtility.SelectAllIdName();
        //    m_customKml = LCustomKml.SelectInspectionKmlSetting();
        //    m_CentralOffices = LCentralOffice.GetAll();
        //}
        
        public static LCacheDataSingleton Instance
        {
            get
            {
                lock (padlock)
                {
                    if (m_instance == null)
                    {
                        m_instance = new LCacheDataSingleton();
                    }
                    return m_instance;
                }
            }
        }

        #region - Private attributes -

        private static List<EXMissionStructures_PoleClass> m_Class;
        private static List<EXMissionStructures_PoleSpecies> m_Specie;
        private static List<EXMissionStructures_PoleTreatment> m_Treatment;
        private static List<EVendor> m_Vendor;
        private static List<EMap> m_Maps;
        private static List<ECompany> m_Companies;
        private static List<ENote> m_Notes;
        private static List<EInspectionTypes> m_InspectionTypes;
        private static List<EResultType> m_ResultTypes;
        private static List<EHandheld> m_Handhelds;
        private static List<EVendor_Visit_Type> m_VisitTypes;
        private static List<EXMissionStructures_TopType> m_TopTypes;
        private static List<EVendor_Contact> m_Contacts;
        private static List<EVendor_Project> m_Projects;
        private static List<EUtility_Polygon> m_Polygons;
        private static List<EUtility> m_Utilities;
        private static ESettingsCustomInspectionKml m_customKml;
        private static EUser m_LoggedUser;
        private static List<ECentralOffice> m_CentralOffices;

        private Thread thread;
        private Thread thread1;
        private Thread thread2;
        //private Thread thread3;

        #endregion

        #region - Public properties -

        public ESettingsCustomInspectionKml CustomKml
        {
            get
            {
                while (thread1.IsAlive) { };
                
                return m_customKml;
            }
        }

        public List<EXMissionStructures_PoleClass> Class
        {
            get
            {
                while (thread.IsAlive){};
                
                return m_Class;
            }
            set { m_Class = value; }
        }

        public List<EXMissionStructures_PoleSpecies> Specie
        {
            get
            {
                while (thread.IsAlive){};

                return m_Specie;
            }
            set { m_Specie = value; }
        }

        public List<EXMissionStructures_PoleTreatment> Treatment
        {
            get
            {
                while (thread.IsAlive) { };
                return m_Treatment;
            }
            set { m_Treatment = value; }
        }

        public List<EVendor> Vendor
        {
            get
            {
                while (thread.IsAlive) { };
                return m_Vendor;
            }
            set { m_Vendor = value; }
        }

        public List<EMap> Maps
        {
            get
            {
                while (thread.IsAlive) { };
                return m_Maps;
            }
            set { m_Maps = value; }
        }

        public List<ECompany> Companies
        {
            get
            {
                while (thread.IsAlive) { };
                return m_Companies;
            }
            set { m_Companies = value; }
        }

        public List<ENote> Notes
        {
            get
            {
                while (thread.IsAlive) { };
                return m_Notes;
            }
            set { m_Notes = value; }
        }

        public List<EInspectionTypes> InspectionTypes
        {
            get
            {
                while (thread.IsAlive) { };
                return m_InspectionTypes;
            }
            set { m_InspectionTypes = value; }
        }

        public List<EResultType> ResultTypes
        {
            get
            {
                while (thread.IsAlive) { };
                return m_ResultTypes;
            }
            set { m_ResultTypes = value; }
        }

        public List<EHandheld> Handhelds
        {
            get
            {
                while (thread.IsAlive) { };
                return m_Handhelds;
            }
            set { m_Handhelds = value; }
        }

        public List<EVendor_Visit_Type> VisitTypes
        {
            get
            {
                while (thread1.IsAlive) { };
                return m_VisitTypes;
            }
            set { m_VisitTypes = value; }
        }

        public List<EXMissionStructures_TopType> TopTypes
        {
            get
            {
                while (thread1.IsAlive) { };
                return m_TopTypes;
            }
            set { m_TopTypes = value; }
        }

        public List<EVendor_Project> Projects
        {
            get
            {
                while (thread1.IsAlive) { };
                return m_Projects;
            }
            set { m_Projects = value; }
        }

        public List<EVendor_Contact> Contacts
        {
            get
            {
                while (thread1.IsAlive) { };
                return m_Contacts;
            }
            set { m_Contacts = value; }
        }

        public List<EUtility_Polygon> Polygons
        {
            get
            {
                while (thread1.IsAlive) { };
                return m_Polygons;
            }
        }


        public List<EUtility> Utilities
        {
            get
            {
                while (thread1.IsAlive) { };
                return m_Utilities;
            }
        }

        public EUser LoggedUser
        {
            get { return m_LoggedUser; }
            set { m_LoggedUser = value; }
        }


        public List<ECentralOffice> CentralOffices
        {
            get
            {
                while (thread1.IsAlive) { };
                return m_CentralOffices;
            }
            set { m_CentralOffices = value; }
        }

        #endregion
    }
}
